package basics.scanner.factory;

import basics.scanner.execute.AnnotationScanner;
import basics.scanner.model.ActionEntity;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author Ldanniel
 */
public class ActionHandleFactory implements Callable<Map<String, Object>> {

    private Map<String, Object> paramMap;
    private String policy;

    public ActionHandleFactory(Map<String, Object> paramMap, String policy) {
        this.paramMap = paramMap;
        this.policy = policy;
    }

    @Override
    public Map call() throws Exception {
        return annotationScanner(paramMap, policy);
    }

    /**
     * 扫描注解的入口，统一由工厂处理
     *
     * @throws Exception
     */

    public Map<String, Object> annotationScanner(Map<String, Object> paramMap, String policy) throws Exception {

        AnnotationScanner annotationScanner = new AnnotationScanner();
        Map<String, ActionEntity> scanner = annotationScanner.scanner();

        ActionEntity actionEntity = scanner.get(policy);
        Class<?> klass = actionEntity.getKlass();
        Method method = klass.getMethod(actionEntity.getMethod().getName(), Map.class);
        Object invoke = method.invoke(actionEntity.getExecuteMethod(), paramMap);

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("threadName",Thread.currentThread().getName());
        resultMap.put("invoke",invoke);
        resultMap.put("status","当前任务已经执行完毕");
         System.out.println(resultMap);
        return resultMap;
    }
}
